<template>
  <el-card shadow="hover">
    <template #header>
      <el-button type="primary" @click="showAdd">添加</el-button>
    </template>

    <el-table row-key="id" border :data="trademarkList">
      <el-table-column label="序号" type="index" width="80" align="center" />

      <el-table-column prop="tmName" label="品牌名称" />

      <el-table-column label="品牌LOGO">
        <template v-slot="{ row }">
          <el-image :src="row.logoUrl" style="width: 100px; height:60px;" />
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template v-slot="{ row, $index }">
          <el-button
            title="修改"
            type="warning"
            size="small"
            class="el-icon-edit"
            @click="showUpdate(row)"
          ></el-button>
          <el-button
            title="删除"
            type="danger"
            class="el-icon-delete-solid"
            size="small"
            @click="remove(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      style="margin-top: 20px; text-align: center;"
      :page-sizes="[14,3, 6, 9]"
      :total="total"
      layout="prev, pager, next, jumper, ->, sizes, total"
      @current-change="val => getTrademarkList(val, pageSize)"
      @size-change="val => getTrademarkList(1, val)"
    />

    <el-dialog
      :title="trademark.id ? '修改品牌' : '添加品牌'"
      :visible.sync="dialogVisible"
    >
      <el-form
        ref="formRef"
        :model="trademark"
        style="width: 80%"
        label-width="100px"
      >
        <el-form-item label="品牌名称" prop="tmName">
          <el-input v-model="trademark.tmName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO" prop="logoUrl">
          <el-upload
            class="avatar-uploader"
            :action="`${BASE_URL}/admin/product/fileUpload`"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            list-type="picture"
          >
            <!-- 添加框 -->

            <!-- 显示的图片 -->
            <img
              v-if="trademark.logoUrl"
              :src="trademark.logoUrl"
              class="avatar"
            />

            <i
              v-else
              class="avatar-uploader-icon"
              :class="uploadLoading ? 'el-icon-loading' : 'el-icon-plus'"
            >
            </i>

            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addOrUpdate">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  reqTrademark,
  reqSaveTrademark,
  reqDeleteTrademark,
  reqUpdateTrademark
} from "@/api/trademark";
import { UploadProps } from "element-ui";
export default {
  name: "trademark",
  data() {
    return {
      trademarkList: [],
      // 当前页码
      currentPage: 1,
      //  每页条数
      pageSize: 14,
      //总条数
      total: 0,
      dialogVisible: false,
      BASE_URL: "http://gmall-h5-api.atguigu.cn",
      // 图片是否在上传中
      uploadLoading: false,
      // 收集表单数据
      trademark: {
        tmName: "",
        logoUrl: ""
      }
    };
  },

  methods: {
    async getTrademarkList(page = this.currentPage, limit = this.pageSize) {
      const re = await reqTrademark(page, limit);
      this.currentPage = page;
      this.pageSize = limit;
      this.trademarkList = re.records;
      this.total = re.total;

     
    },

    handleAvatarSuccess(res, file) {
      this.trademark.logoUrl = res.data;
      this.uploadLoading = false;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      this.uploadLoading = true;
      return isJPG && isLt2M;
    },
    // 点击添加按钮
    showAdd() {
      this.trademark = {
        tmName: "",
        logoUrl: ""
      };
      // 显示dialog
      this.dialogVisible = true;
    },
    // 点击修改按钮
    showUpdate(row) {
      // const { id, tmName, logoUrl } = row;
      // this.trademark.id = id;
      // this.trademark.tmName = tmName;
      // this.trademark.logoUrl = logoUrl;
      this.trademark={...row}
    
      // 显示dialog
      this.dialogVisible = true;
    },
    // 添加或修改品牌
    async addOrUpdate() {
      const { trademark } = this;
      if (trademark.id) {
        await reqUpdateTrademark(trademark);
      } else {
        await reqSaveTrademark(trademark);
      }

      // 如果成功, 提示成功, 并获取新的列表显示
      this.$message.success("保存成功!");
      // 关闭当前dialog
      this.dialogVisible = false;
      this.getTrademarkList(this.trademark.id ? this.currentPage : 1);
    },
    remove(row) {
      this.$confirm(`确定删除 ${row.tmName} 吗?`)
        .then(async () => {
          await reqDeleteTrademark(row.id);
          this.$message.success("删除成功!");
          if (this.trademarkList.length === 1 && this.currentPage > 1) {
            this.currentPage -= 1;
          }
          getTrademarkList();
        })
        .catch(error => {
          if (error === "cancel") {
            this.$message.success("取消删除");
          }
        });
    }
  },
  mounted() {
    this.getTrademarkList();
    this.$message.success("获取品牌分页列表成功");
  },
  computed: {},

  watch: {
    currentPage() {
      this.getTrademarkList();
    },
    pageSize() {
      this.getTrademarkList();
    }
  }
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
